package com.yunkeji.framework.config;

import cn.hutool.core.bean.BeanUtil;
import com.yunkeji.common.filter.CryptoFilter;
import com.yunkeji.common.filter.RepeatableFilter;
import com.yunkeji.common.filter.TokenAutoRenew;
import com.yunkeji.common.filter.XssFilter;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.framework.config.properties.ApiDecryptProperties;
import com.yunkeji.framework.config.properties.XssProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.DispatcherType;
import java.util.HashMap;
import java.util.Map;

/**
 * Filter配置
 *
 * @author Lion Li
 */
@Configuration
@Slf4j
public class FilterConfig {

    @Autowired private XssProperties xssProperties;

    @Value("${sa-token.timeout}")
    private Integer saTokenExpire;

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RepeatableFilter());
        registration.addUrlPatterns("/*");
        registration.setName("repeatableFilter");
        registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
        return registration;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Bean
    @ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
    public FilterRegistrationBean xssFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new XssFilter());
        registration.addUrlPatterns(
                StringUtils.split(xssProperties.getUrlPatterns(), StringUtils.SEPARATOR));
        registration.setName("xssFilter");
        registration.setOrder(2);
        Map<String, String> initParameters = new HashMap<String, String>();
        initParameters.put("excludes", xssProperties.getExcludes());
        registration.setInitParameters(initParameters);
        return registration;
    }

    /**
     * 接口加密解密拦截器
     *
     * @param apiDecryptProperties
     * @return
     */
    @Bean
    @ConditionalOnProperty(value = "apidecrypt.enabled", havingValue = "true")
    public FilterRegistrationBean cryptoFilterRegistration(
            ApiDecryptProperties apiDecryptProperties) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        CryptoFilter cryptoFilter =
                new CryptoFilter(
                        BeanUtil.toBean(apiDecryptProperties, CryptoFilter.CryptoConfig.class));
        registration.setFilter(cryptoFilter);
        registration.addUrlPatterns("/*");
        registration.setName("cryptoFilter");
        registration.setOrder(3);
        return registration;
    }

    /**
     * Token 自动续签
     *
     * @param apiDecryptProperties
     * @return
     */
    @Bean
    @ConditionalOnProperty(value = "apidecrypt.enabled", havingValue = "true")
    public FilterRegistrationBean autoRenew(ApiDecryptProperties apiDecryptProperties) {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        TokenAutoRenew tokenAutoRenew = new TokenAutoRenew(saTokenExpire);
        registration.setFilter(tokenAutoRenew);
        registration.addUrlPatterns("/*");
        registration.setName("autoRenew");
        registration.setOrder(4);
        return registration;
    }
}
